import java.util.*;

public class Solution218 {

    static class pair{
        int pos;
        int height;
        int up;

        public pair(int pos, int height, int up) {
            this.pos = pos;
            this.height = height;
            this.up = up;
        }
    }

    public List<List<Integer>> getSkyline(int[][] buildings) {
        List<pair> list=new ArrayList<>();
        for (int[] building : buildings) {
            list.add(new pair(building[0],building[2],1));
            list.add(new pair(building[1],building[2],-1));
        }
        Collections.sort(list, Comparator.comparingInt(a -> a.pos));
        Set<Integer> set=new HashSet<>();
        for (int i=0;i<list.size()-1;i++){
            if (list.get(i).pos!=list.get(i+1).pos){
                set.add(i);
            }
        }
        set.add(list.size()-1);
        TreeMap<Integer,Integer> map=new TreeMap<>();
        map.put(0,1);
        int cur=0;
        List<List<Integer>> ans=new ArrayList<>();

        for (int i=0;i<list.size();i++) {
            pair pair=list.get(i);
            map.put(pair.height,map.getOrDefault(pair.height,0)+pair.up);
            if (map.get(pair.height)==0){
                map.remove(pair.height);
            }
            if (set.contains(i)){
                if (cur!=map.lastKey()) {
                    cur = map.lastKey();
                    List<Integer> l = new ArrayList<>();
                    l.add(pair.pos);
                    l.add(cur);
                    ans.add(l);
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution218().getSkyline(new int[][]{{0,2,3},{2,5,3}}));
    }
}
